<?php
/*
    ainitramfs base script
    Copyright (C) Ambroz Bizjak, 2010

    This file is part of ainitramfs.

    ainitramfs is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 2
    as published by the Free Software Foundation.

    ainitramfs is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

require_once("{$ai_dataroot}/scripts_common.php");

function base_install ()
{
    global $ai_root;
    global $ai_out;
    global $ai_kmoddir;
    global $ai_kmodroot;
    global $ai_dataroot;
    global $ai_kmods;
    global $ai_options;
    global $ai_toolchain;

    $options = array(
        "network" => 0,
        "nfs" => 0,
        "nbd" => 0,
        "crypt" => 0,
    );

    // read options
    if (array_key_exists("base", $ai_options)) {
        foreach ($ai_options["base"] as $name => $val) {
            if (!array_key_exists($name, $options)) {
                ai_error("unknown base option ${name}");
            }

            if ($val == "yes") {
                $options[$name] = 1;
            }
            else if ($val == "no") {
                $options[$name] = 0;
            }
            else {
                ai_error("unknown value for base option ${name}");
            }
        }
    }

    // accept options
    unset($ai_options["base"]);

    $dirs = array();
    $files = array();
    $symlinks = array();
    $binaries = array();
    $kmod_dirs = array();
    $kmods = array();

    if ($options["network"]) {
        $binaries = array_merge($binaries, array(
            "ifconfig",
            "route",
        ));

        $kmod_dirs = array_merge($kmod_dirs, array(
            "kernel/drivers/net",
        ));
    }

    if ($options["nfs"]) {
        $binaries = array_merge($binaries, array(
            "/sbin/mount.nfs",
        ));

        $kmods = array_merge($kmods, array(
            "nfs",
        ));
    }

    if ($options["nbd"]) {
        $binaries = array_merge($binaries, array(
            "/usr/sbin/nbd-client",
        ));

        $kmods = array_merge($kmods, array(
            "nbd",
        ));

        $symlinks = array_merge($symlinks, array(
            array("/bin/@nbd-client", "/usr/sbin/nbd-client"),
        ));
    }

    if ($options["crypt"]) {
        ai_enable_option("common", "device_mapper");
        ai_enable_option("common", "input");

        $binaries = array_merge($binaries, array("cryptsetup"));
        $kmods = array_merge($kmods, array("dm-crypt"));
        $kmod_dirs = array_merge($kmod_dirs, array("kernel/crypto"));
    }

    // invoke common
    ai_scripts_common($dirs, $files, $symlinks, $binaries, $kmod_dirs, $kmods);

    // copy init
    $initdest = ai_join_paths($ai_out, "init");
    if (!copy(ai_join_paths($ai_dataroot, "init.sh"), $initdest)) {
        ai_error("cannot copy init");
    }
    if (!chmod($initdest, 0755)) {
        ai_error("cannot chmod init");
    }

    // compile and install getkernelarg
    if (ai_invoke_shell("{$ai_toolchain}gcc ".escapeshellarg(ai_join_paths($ai_dataroot, "getkernelarg.c"))." -o ".escapeshellarg(ai_join_paths($ai_out, "/bin/getkernelarg"))) != 0) {
        ai_error("cannot compile getkernelarg");
    }
    if (ai_invoke_shell("{$ai_toolchain}strip ".escapeshellarg(ai_join_paths($ai_out, "/bin/getkernelarg"))) != 0) {
        ai_error("cannot install getkernelarg");
    }
}

base_install();
